package com.igeek.blog_parent_test.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.blog_parent_test.service.ICommentsService;
import com.igeek.blog_parent_test.constant.MessageConstant;
import com.igeek.blog_parent_test.entity.Comments;
import com.igeek.blog_parent_test.result.Result;
import com.igeek.blog_parent_test.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 评论前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-04
 */
@RestController
@RequestMapping("admin/comments")
@Api(tags = "留言接口管理")
public class CommentsController {
    @Autowired
    private ICommentsService ICommentsService;

    @ApiOperation("分页条件查询留言")
    @GetMapping("findPage")
    public Result findPage(@ApiParam("分页参数") PageQueryVO vo) {
        Page<Comments> page = ICommentsService.findPage(vo);
        if (page != null) {
            return new Result(true, MessageConstant.QUERY_COMMENTS_SUCCESS, page);
        }
        return new Result(false, MessageConstant.QUERY_COMMENTS_FAIL);
    }

    @ApiOperation("新增留言")
    @PostMapping("add")
    public Result add(@RequestBody Comments comments) {
        boolean flag = ICommentsService.save(comments);
        if (flag) {
            return new Result(true, MessageConstant.ADD_COMMENTS_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_COMMENTS_FAIL);
    }

    @ApiOperation("通过ID查询评论信息")
    @GetMapping("{comment_id}")
    public Result findOne(@PathVariable("comment_id") Integer comment_id) {
        Comments comment = ICommentsService.getById(comment_id);
        if (!ObjectUtils.isEmpty(comment)) {
            return new Result(true, MessageConstant.QUERY_COMMENTS_SUCCESS, comment);
        }
        return new Result(false, MessageConstant.QUERY_COMMENTS_FAIL);
    }

    @ApiOperation("编辑评论")
    @PostMapping("update")
    public Result update(@RequestBody Comments comment) {
        boolean flag = ICommentsService.updateById(comment);
        if (flag) {
            return new Result(true, MessageConstant.EDIT_COMMENTS_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_COMMENTS_FAIL);
    }

    @ApiOperation("删除留言")
    @DeleteMapping("{comment_id}")
    public Result delete(@PathVariable("comment_id") Integer comment_id) {
        boolean flag = ICommentsService.del(comment_id);
        if(flag){
            return new Result(true, MessageConstant.DELETE_USERS_SUCCESS);
        }
        return new Result(true, MessageConstant.DELETE_USERS_FAIL);

    }
}